package net.suncapper.wcss.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.suncapper.wcss.common.utls.SessionUtils;
import net.suncapper.wcss.model.domain.WhitelistSetting;
import net.suncapper.wcss.mapper.WhitelistSettingMapper;
import net.suncapper.wcss.model.dto.PageDTO;
import net.suncapper.wcss.model.dto.WhitelistSettingDTO;
import net.suncapper.wcss.model.vo.WhitelistSettingVO;
import net.suncapper.wcss.service.IStudentDataService;
import net.suncapper.wcss.service.IUserManageService;
import net.suncapper.wcss.service.IWhitelistSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 学校用户白名单设置 服务实现类
 * </p>
 *
 * @author ztr
 * @since 2025-10-30
 */
@Service
public class WhitelistSettingServiceImpl extends ServiceImpl<WhitelistSettingMapper, WhitelistSetting> implements IWhitelistSettingService {
@Autowired
    private WhitelistSettingMapper whitelistSettingMapper;
@Autowired
private IUserManageService userManageService;
@Autowired
private IStudentDataService studentDataService;
    @Override
    public WhitelistSetting login(WhitelistSetting whitelistSetting) {
        WhitelistSetting one=
          whitelistSettingMapper.selectOne(Wrappers.<WhitelistSetting>lambdaQuery()
                .eq(WhitelistSetting::getSn,whitelistSetting.getSn())
               .eq(WhitelistSetting::getPassword,whitelistSetting.getPassword())
                .eq(WhitelistSetting::getEnabled,Boolean.TRUE));
           SessionUtils.setCurrentLoginUser(one);
        return one;
    }

    @Override
    public Page<WhitelistSettingVO> pageList(WhitelistSettingDTO dto, PageDTO pageDTO) {
        Page<WhitelistSetting> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        List<WhitelistSettingVO> whitelistSettings = whitelistSettingMapper.selectLists(page, dto);
        // 封装分页结果
        Page<WhitelistSettingVO> resultPage = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        resultPage.setRecords(whitelistSettings);
        resultPage.setTotal(page.getTotal()); // 复用总条数
        return resultPage;
    }

    @Override
    public List<WhitelistSettingVO> getBykey(String key) {
        List<WhitelistSettingVO> list=userManageService.getByKey(key);
        if(CollectionUtils.isNotEmpty(list)){
            return list;
        }
        return studentDataService.getByKey(key);
    }

    @Override
    public Integer insert(WhitelistSettingVO key) {
        Integer whitelistSettingVO=userManageService.insertByKey(key);
        if(whitelistSettingVO!=null){
            return whitelistSettingVO;
        }
        return studentDataService.insertByKey(key);
    }

    @Override
    public List<WhitelistSetting> saveUser(WhitelistSettingVO wvo) {
        return whitelistSettingMapper.selectBySave(wvo);
    }
}
